package com.sicurezza.nfcrypto;

import android.app.Activity;
import android.app.Fragment;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.progettoSicurezza.secUtility.HashMaker;

public class LoginActivity extends Activity {
	
	private SharedPreferences sp = this.getSharedPreferences("KeyPreferences", MODE_PRIVATE);
	private SharedPreferences.Editor spe = sp.edit();
	private final static String TAG = "LoginActivity";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);

		if (savedInstanceState == null) {
			getFragmentManager().beginTransaction()
					.add(R.id.container, new PlaceholderFragment()).commit();
		}
		
		Button loginButton = (Button) findViewById(R.id.button2);
		loginButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Log.v(TAG, "Inizia il confronto");
				String truePasswd = sp.getString("MasterPassword", null);
				EditText edit = (EditText) findViewById(R.id.editText2);
				String userPasswd = edit.getEditableText().toString();
				Log.v(TAG, "Prese le due password da confrontare");
				HashMaker hm = new HashMaker(userPasswd.getBytes(), "SHA-256", getApplicationContext());
				if(!truePasswd.equals(hm.hashMake())){
					Toast.makeText(getBaseContext(), "Hai inserito un password sbagliata", Toast.LENGTH_LONG).show();
					Log.w(TAG, "Inserita password errata");
				}
				else{
					spe.putBoolean("logged", true);
					spe.commit();
					Log.v(TAG, "Inserita password corretta, mostriamo l'applicazione");
					Intent redirectIntent = new Intent();
					redirectIntent.setClassName("com.progettoSicurezza.nfcrypto", "MainActivity");
					startActivity(redirectIntent);
				}
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.login, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	/**
	 * A placeholder fragment containing a simple view.
	 */
	public static class PlaceholderFragment extends Fragment {

		public PlaceholderFragment() {
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			View rootView = inflater.inflate(R.layout.fragment_login,
					container, false);
			return rootView;
		}
	}

}
